<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Category;
use think\Validate;
use think\Db;
class Cates extends Controller
{
	public function cateList($id)
	{
		if($id==0){
			$res = Db::name('category')->where('pid','0')->order('id asc')->paginate(4);
			$count = Category::where('pid','0')->count();
		}else{
			$res = Db::name('category')->where('pid',$id)->order('id asc')->paginate(4);
			$count = Category::where('pid',$id)->count();
		}
		$this->assign('list',$res);
		$this->assign('count',$count);
		$empty = '<td colspan=7 style="font-size:20px;color:#f00;text-align:center;">暂无数据,请添加...<td>';
		$this->assign('empty',$empty);
		return $this->fetch('cate/admin-cate');
	}
	//添加一级分类
	public function addCate()
	{
		$data = input('post.');
		if( strlen($data['name'])<1 || strlen($data['name'])>20 ){
			$arr = ['code'=>0,'msg'=>'长度不符合'];
		} else {
			if(Db::name('category')->where('name',$data['name'])->find()){
					$arr = ['code'=>'0','msg'=>'分类名已存在'];
			}else{
				$data['pid'] = 0;
				$data['path'] = '0,';
				$data['display'] = 1;
				$row = DB::name('category')->insertGetId(["name"=>$data['name'],"pid"=>$data['pid'],"path"=>$data['path'],"display"=>$data['display']]);
				if($row){
	    			$arr = ['code'=>'1','msg'=>'添加成功','name'=>$data['name'],'lastid'=>$row];
	    		}else{
	    			$arr = ['code'=>'0','msg'=>'添加失败'];
	    		}
			}
			
		}
		return json($arr);
	}
	public function showAddSonCates($id)
	{
		// dump($id);die;
		$data = Db::name('category')->field('pid,path,id')->where('id',$id)->find();
		$this->assign('pid',$data['pid']);
		$this->assign('path',$data['path']);
		$this->assign('id',$data['id']);
		return $this->fetch('cate/cate-add');
	}

	public function addSonCate($id)
	{
		$ajaxpost = input('post.');
		$data = Db::name('category')->field('pid,path,id')->where('id',$id)->find();
		$name = $ajaxpost['catename'];
		$pid = $data['id'];
		$path = $data['path'].$pid.',';
		$display = $ajaxpost['display'];
		if(Db::name('category')->where('name',$name)->find()){
					$arr = ['code'=>'0','msg'=>'分类名已存在'];
		}else{
			
			$row = DB::name('category')->insertGetId(["name"=>$name,"pid"=>$pid,"path"=>$path,"display"=>$display]);
			$arr = ['code'=>'0','msg'=>'成功'];
		}
		return json($arr);
		
	}

	public function delCate($id)
	{
		if(Db::name('category')->where('pid',$id)->select()){
			$info['status'] = 0;
            $info['info'] = '存在子分类,请先删除子分类';
		}else{
			$result = Db::name('category')->delete($id);
	         if ($result > 0) {
	            $info['status'] = 1;
	            $info['id'] = $id;
	            $info['info'] = '分类删除成功!';
	        } else {
	            $info['status'] = 0;
	            $info['id'] = $id;
	            $info['info'] = '分类删除失败!';
	        }
		}
		
        return json($info);
	}

	public function showEdit($id)
	{
		$data = Db::name('category')->field('pid,path,id,name,display')->where('id',$id)->find();
		$this->assign('pid',$data['pid']);
		$this->assign('path',$data['path']);
		$this->assign('id',$data['id']);
		$this->assign('name',$data['name']);
		$this->assign('display',$data['display']);
		return $this->fetch('cate/cate-edit');
	}

	public function editcate($id)
	{
		$ajaxpost = input('post.');
		$name = $ajaxpost['catename'];
		$display = $ajaxpost['display'];
		$row = DB::name('category')->where('id',$id)->update(["name"=>$name,"display"=>$display]);
		return json(['code'=>1,'name'=>$name]);
	}
}